/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab-modules.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

//===========================================================================================
#include "Ogre_Gfx.h"

//===========================================================================================
dylab::Ogre_Gfx::Window::Window(const String & _title, Ogre_Gfx * _gfx)
: mViewportMap(true), mDefaultViewport(NULL), mGfx(_gfx)
{
    // check the arguemtns
    DYLAB_ASSERT(mGfx != NULL);

    // initiase the ogre window (only one window supported at this time)
    mOgreWindow = Ogre::Root::getSingleton().initialise(true, _title);
    DYLAB_ASSERT(mOgreWindow != NULL);    
}
//-----------------------------------------------------------------------------
dylab::Ogre_Gfx::Window::~Window()
{
    mViewportMap.removeAll();
}
//-----------------------------------------------------------------------------
dylab::String dylab::Ogre_Gfx::Window::getName() const
{
    return mOgreWindow->getName();
}
//-----------------------------------------------------------------------------
void dylab::Ogre_Gfx::Window::getMetrics(uint32_t & _width, uint32_t & _height, uint32_t & _colourDepth, int32_t & _left, int32_t & _top) const
{
    unsigned int width, height, colourDepth;
    int left, top;

    mOgreWindow->getMetrics(width, height, colourDepth, left, top);
    
    _width = width;
    _height = height;
    _colourDepth = colourDepth;
    _left = left;
    _top = top;
}
//-----------------------------------------------------------------------------
void dylab::Ogre_Gfx::Window::getStatistics(uint32_t & _lastFPS, uint32_t & _bestFPS, uint32_t & _worstFPS, uint32_t & _triangleCount) const
{
    const Ogre::RenderTarget::FrameStats& stats = mOgreWindow->getStatistics();

    _lastFPS = static_cast<uint32_t>(stats.lastFPS);
    _bestFPS = static_cast<uint32_t>(stats.bestFPS);
    _worstFPS = static_cast<uint32_t>(stats.worstFPS);
    _triangleCount = static_cast<uint32_t>(stats.triangleCount);
}
//-----------------------------------------------------------------------------
void dylab::Ogre_Gfx::Window::getPlatformHandle(void * _handle) const
{
    // check the attribute
    DYLAB_ASSERT(_handle != NULL);

    // retrieve the window handle
    mOgreWindow->getCustomAttribute("WINDOW", _handle);
}
//-----------------------------------------------------------------------------
void dylab::Ogre_Gfx::Window::setPosition(const Vector2 & _pos)
{
    mOgreWindow->reposition(_pos.x(), _pos.y());    
}
//-----------------------------------------------------------------------------
dylab::Ogre_Gfx::IViewport * 
dylab::Ogre_Gfx::Window::createViewport(uint32_t _zOrder, ICamera * _renderCamera, const Vector2 & _position, const Vector2 & _size)
{
    // determine the render camera
    ICamera * camera = _renderCamera;
    if (camera == NULL)
        camera = mGfx->getScene()->getCamera();

    Viewport * viewport = new Viewport(_zOrder, this, camera, _position.x(), _position.y(), _size.x(), _size.y());
    DYLAB_ASSERT(viewport != NULL);

    mViewportMap.insert(_zOrder, viewport);

    if (mDefaultViewport == NULL)
        mDefaultViewport = viewport;

    return viewport;
}
//-----------------------------------------------------------------------------
dylab::Ogre_Gfx::IViewport * 
dylab::Ogre_Gfx::Window::getViewport(uint32_t _zOrder)
{
    if (_zOrder == IViewport::DEFAULT_ZORDER)
    {
        DYLAB_ASSERT(mDefaultViewport);
        return mDefaultViewport;
    }
    else
    {
        return mViewportMap.get(_zOrder);
    }
}
//-----------------------------------------------------------------------------
const dylab::Ogre_Gfx::IViewport * 
dylab::Ogre_Gfx::Window::getViewport(uint32_t _zOrder) const
{
    if (_zOrder == IViewport::DEFAULT_ZORDER)
    {
        DYLAB_ASSERT(mDefaultViewport);
        return mDefaultViewport;
    }
    else
    {
        return mViewportMap.get(_zOrder);
    }
}
//-----------------------------------------------------------------------------
void dylab::Ogre_Gfx::Window::destroyViewport(uint32_t _zOrder)
{
    uint32_t removeZOrder = _zOrder;

    if (_zOrder == IViewport::DEFAULT_ZORDER)
    {
        DYLAB_ASSERT(mDefaultViewport != NULL);
        removeZOrder = mDefaultViewport->getZOrder();
    }
    
    mViewportMap.remove(removeZOrder);
    mOgreWindow->removeViewport(removeZOrder);
}
//-----------------------------------------------------------------------------